package cn.buk.api.eotms.dto;

import cn.buk.common.flight.dto.FlightInfoDto;
import cn.buk.tms.common.dto.flight.BaseFlightOrderPassengerDto;
import cn.buk.tms.common.dto.flight.BaseFlightOrderResponseDto;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.buk.tms.common.constant.TmsOrderConstant.*;

/**
 * @author yfdai
 */
public class FlightOrderResponse extends BaseFlightOrderResponseDto {

    /**
     * 出票时限
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date tktlDate;

    /**
     * 出票日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date etdzDate;

    /**
     * 机票订单的使用状态
     */
    private Integer showStatus;

    /**
     * 航班信息
     */
    private List<FlightInfoDto> flights;

    /**
     * 乘机人信息
     */
    private List<FlightOrderPassengerDto> passengers;

    /**
     * 票号信息
     */
    private List<FlightOrderTicketDto> tickets;






    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    public String getShowStatusDesc() {
        if (this.showStatus == null) {
            return null;
        }
        switch (this.showStatus) {
            case FLIGHT_ORDER_TICKET_WHOLE_USED:
                return "已使用";
            case FLIGHT_ORDER_TICKET_WHOLE_REFUNDED:
                return "已退票";
            case FLIGHT_ORDER_TICKET_WHOLE_EXCHANGED:
                return "已改签";
            case FLIGHT_ORDER_TICKET_PARTIAL_USED
                    + FLIGHT_ORDER_TICKET_PARTIAL_EXCHANGED:
                return "部分使用改签";
            case FLIGHT_ORDER_TICKET_PARTIAL_USED
                    + FLIGHT_ORDER_TICKET_PARTIAL_REFUNDED:
                return "部分使用退票";
            case FLIGHT_ORDER_TICKET_PARTIAL_EXCHANGED
                    + FLIGHT_ORDER_TICKET_PARTIAL_REFUNDED:
                return "部分改签退票";
            case FLIGHT_ORDER_TICKET_PARTIAL_USED
                    + FLIGHT_ORDER_TICKET_PARTIAL_EXCHANGED
                    + FLIGHT_ORDER_TICKET_PARTIAL_REFUNDED:
                return "部分使用改签退票";
            default:
                return "";
        }
    }


    public List<FlightInfoDto> getFlights() {
        if (flights == null) {
            flights = new ArrayList<>();
        }
        return flights;
    }

    public void setFlights(List<FlightInfoDto> flights) {
        this.flights = flights;
    }

    public List<FlightOrderPassengerDto> getPassengers() {
        if (passengers == null) {
            passengers = new ArrayList<>();
        }
        return passengers;
    }

    public void setPassengers(List<FlightOrderPassengerDto> passengers) {
        this.passengers = passengers;
    }


    public Date getTktlDate() {
        return tktlDate;
    }

    public void setTktlDate(Date tktlDate) {
        this.tktlDate = tktlDate;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getEtdzDate() {
        return etdzDate;
    }

    public void setEtdzDate(Date etdzDate) {
        this.etdzDate = etdzDate;
    }


    public Integer getShowStatus() {
        return showStatus;
    }

    public void setShowStatus(int showStatus) {
        this.showStatus = showStatus;
    }

    public List<FlightOrderTicketDto> getTickets() {
        if (tickets == null) {
            tickets = new ArrayList<>();
        }
        return tickets;
    }

    public void setTickets(List<FlightOrderTicketDto> tickets) {
        this.tickets = tickets;
    }

    public void processPassengerTicketStatus() {
        for(BaseFlightOrderPassengerDto psg: this.getPassengers()) {
            int ticketStatus = -1;
            for(FlightOrderTicketDto ticket: this.getTickets()) {
                if (!ticket.getPassengerName().equalsIgnoreCase(psg.getName())) {
                    continue;
                }

                if (ticket.getTicketStatus() == null) {
                    ticketStatus = -1;
                    break;
                }

                if (ticket.getTicketStatus() == 1
                        || ticket.getTicketStatus() == 2
                        || ticket.getTicketStatus() == 3
                        || ticket.getTicketStatus() == 4
                        || ticket.getTicketStatus() == 128) {
                    ticketStatus = 1;
                } else  {
                    ticketStatus = -1;
                    break;
                }
            }

            if (ticketStatus >= 0) {
                psg.setTicketStatus(ticketStatus);
            }
        }

    }
}
